﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace GameWP
{
    public class AutoCharacter : Character
    {
    #region Properties
        protected SpriteEffects _spriteEffect;
        public SpriteEffects SpriteEffect
        {
            get { return _spriteEffect; }
            set { _spriteEffect = value; }
        }      

        protected Character _characterWasHit;
        protected Character CharacterWasHit
        {
            get { return _characterWasHit; }
            set { _characterWasHit = value; }
        }

        protected Arrow _arrow;
        public Arrow Arrow
        {
            get { return _arrow; }
            set { _arrow = value; }
        }

        protected Vector2 _positionBullet;
        public Vector2 PositionBullet
        {
            get { return _positionBullet; }
            set { _positionBullet = value; }
        }

        protected bool _onlyAttackTresure = false;
        public bool OnlyAttackTresure
        {
            get { return _onlyAttackTresure; }
            set { _onlyAttackTresure = value; }
        }

        protected bool _isDoctor = false;
        public bool IsDoctor
        {
            get { return _isDoctor; }
            set { _isDoctor = value; }
        }
       
    #endregion

    #region  Methods
        public virtual bool CheckAttack(Character characterIsAttacked, GameTime gametime)
        {
            bool attack = false;
            int a;
            if (this.Position.X >= 40 && this.Position.X <= 1850)
            {
                if (_state != State.Disappear)
                {
                    if (OnlyAttackTresure)
                        a = 0;
                    if (this.IsCollisionWith(characterIsAttacked))
                    {
                        attack = true;
                        this.State = State.Fight;
                        _characterWasHit = characterIsAttacked;
                        double gt = gametime.TotalGameTime.TotalMilliseconds;
                        if ((gt % characterIsAttacked.NormalDelay) < characterIsAttacked.Delay)
                            if (_indexTexture >= _seqTexture[(int)State.Fight][(int)(_seqTexture[(int)State.Fight].Count / 3)])
                                characterIsAttacked.Hp -= this.Damage;
                    }
                    if (!attack) this.State = State.Move;
                }
            }            
            return attack;
        }

        public virtual void Reset()
        {

        }
    #endregion

    #region Initiation       
        public AutoCharacter(List<Texture2D> listTextures,
            Vector2 posotion, 
            int hp, 
            int damage, 
            float speed,
            float delay,
            float scaleTexture,
            int price,
            SpriteEffects spriteEffect,
            List<List<int>> seqTexture): base(listTextures, posotion, hp, damage, speed, delay, scaleTexture, price, seqTexture)          
        {
            _listTextures = listTextures;
            _position = posotion;
            _hp = hp; _hpmax = hp;
            _damage = damage;
            _speed = speed;
            _delay = delay;
            _price = price;
            _seqTexture = seqTexture;
            _rectangle = new Rectangle
                ((int)_position.X, (int)_position.Y, _listTextures[0].Width, _listTextures[0].Height);
            _scaleRect = -2;
            _scaleTexture = scaleTexture;
            _spriteEffect = spriteEffect;
            _indexTexture = 0;
            _state = State.Move;
            _preState = State.Unknow;
            
            //HpBar
            _hpBar = new HpBar(_hp, new Vector2(_position.X + _listTextures[0].Width, _position.Y + 20), Color.Red);
        }    
    #endregion

    #region Update & Draw
        public override void Update(GameTime gametime, Vector2 position)
        {
            if (_state != State.Disappear)
            {
                double gt = gametime.TotalGameTime.Milliseconds;
                _hpBar.Hp = _hp;
                if ((gt % NormalDelay) < _delay)
                {
                    if (_hp <= 0)
                    {
                        _state = State.Dead;
                        if (_scaleTexture <= 0)
                        {
                            _state = State.Disappear;
                            _rectangle.X = -99999;
                            return;
                        }
                    }
                    if (_preState == _state)
                    {
                        _indexTexture += 1;
                        switch (_state)
                        {
                            case State.Move:
                                _position.X -= _speed;
                                _hpBar.PositionX = _position.X + _listTextures[0].Width;
                                _rectangle.X = (int)_position.X;
                                _rectangle.Y = (int)_position.Y;
                                // xem xet lai
                                if (_position.X > 2200 || _position.X < -150) _hp = 0;
                                break;
                            case State.Fight:
                                _hpBar.PositionX = _position.X + _listTextures[0].Width;
                                break;
                            case State.Dead:
                                _position.Y += 5;
                                _scaleTexture -= 0.2f;
                                break;
                        }
                        int intState = (int)_state;
                        if (_indexTexture > _seqTexture[intState][_seqTexture[intState].Count - 1])
                            _indexTexture = _seqTexture[intState][0];
                    }
                    else
                    {
                        switch (_state)
                        {
                            case State.Move:
                                break;
                            case State.Fight:
                                if (_arrow != null)
                                    _arrow.PositionX = _position.X;
                                break;
                            case State.Dead:
                                _position.Y += _rectangle.Height / 2;
                                break;
                        }
                        _indexTexture = _seqTexture[(int)_state][0];
                        _preState = _state;
                    }
                }
                _hpBar.Update(gametime, _hpBar.Position, _hpBar.Hp);
            }            
        }

        public override void Draw(SpriteBatch spritebatch, GameTime gametime)
        {
            //spritebatch.Draw(_listTextures[_indexTexture], _position, Color.White);
            if (_state != State.Disappear)
            {
                spritebatch.Draw(_listTextures[_indexTexture], _position, null, _colorTexture, 0, Vector2.Zero, _scaleTexture, _spriteEffect, 0);
                _hpBar.Draw(spritebatch, gametime);
            }
        }
    #endregion
    }
}
